<html>
<body>

<?php include 'mainheader.php'; ?>
<?php include 'databaseutil.php'; ?>


<?php

// Main

// Verifica se ja deve fazer o update
if( isset($_GET["update"]) )
{
	// Fazendo o update
	echo "<br><br>";
	$res = updateAssetValue( "2Qtos C. Grande", $_GET["cg"], $_GET["month"], $_GET["year"] );
	if( $res == 0 )
		return;

	$res = updateAssetValue( "2Qtos Curicica", $_GET["cu"], $_GET["month"], $_GET["year"] );
	if( $res == 0 )
		return;

	$res = updateAssetValue( "GOLD", $_GET["g"], $_GET["month"], $_GET["year"] );
	if( $res == 0 )
		return;

	$res = updateAssetValue( "SILVER", $_GET["s"], $_GET["month"], $_GET["year"] );
	if( $res == 0 )
		return;

	$res = updateAssetValue( "DOW", $_GET["do"], $_GET["month"], $_GET["year"] );
	if( $res == 0 )
		return;

	$res = updateAssetValue( "iBovespa", $_GET["b"], $_GET["month"], $_GET["year"] );
	if( $res == 0 )
		return;

	$res = updateAssetValue( "PETR4", $_GET["p"], $_GET["month"], $_GET["year"] );
	if( $res == 0 )
		return;

	$res = updateAssetValue( "VALE5", $_GET["v"], $_GET["month"], $_GET["year"] );
	if( $res == 0 )
		return;

	$res = updateAssetValue( "OGXP3", $_GET["o"], $_GET["month"], $_GET["year"] );
	if( $res == 0 )
		return;

	$res = updateAssetValue( "USIM5", $_GET["u"], $_GET["month"], $_GET["year"] );
	if( $res == 0 )
		return;

	$res = updateAssetValue( "Dollar", $_GET["dr"], $_GET["month"], $_GET["year"] );
	if( $res == 0 )
		return;

	$res = updateAssetValue( "Euro", $_GET["dr"], $_GET["month"], $_GET["year"] );
	if( $res == 0 )
		return;

	// Se chegou ate aqui, update foi um sucesso!
	echo "Update done!";
	return;
}

// Veririca se deve mostrar o formulario de mes/ano
if( !isset($_GET["month"]) || !isset($_GET["year"]) )
{
	echo "<br><br>";
	echo "<form name='input' action='edit.php' method='get'>";

	echo "Month: "; //<input type='text' name='month'><br>";
	echo "<select name='month'>";
		echo "<option value='1'>January</option>";
		echo "<option value='2'>February</option>";
		echo "<option value='3'>March</option>";
		echo "<option value='4'>April</option>";
		echo "<option value='5'>May</option>";
		echo "<option value='6'>June</option>";
		echo "<option value='7'>July</option>";
		echo "<option value='8'>August</option>";
		echo "<option value='9'>September</option>";
		echo "<option value='10'>October</option>";
		echo "<option value='11'>November</option>";
		echo "<option value='12'>December</option>";
	echo "</select>";
	echo "<br>";
	echo "Year: <input type='text' name='year'><br>";
	echo "<br>";
	echo "<input type='submit' value='Submit'>";
	echo "</form>";
	return;
}

// Get values from form
$month = $_GET["month"];
$year = $_GET["year"];

// Get asset values according to date
$cg = getAssetValue( "2Qtos C. Grande", $month, $year );
$cu = getAssetValue( "2Qtos Curicica", $month, $year );
$g = getAssetValue( "GOLD", $month, $year );
$s = getAssetValue( "SILVER", $month, $year );
$do = getAssetValue( "DOW", $month, $year );
$b = getAssetValue( "iBovespa", $month, $year );
$p = getAssetValue( "PETR4", $month, $year );
$v = getAssetValue( "VALE5", $month, $year );
$o = getAssetValue( "OGXP3", $month, $year );
$u = getAssetValue( "USIM5", $month, $year );
$dr = getAssetValue( "Dollar", $month, $year );
$er = getAssetValue( "Euro", $month, $year );

// Valida as informacoes do banco para evitar divisao por zero
if( $cg == NULL || $cu == NULL || $g == NULL || $s == NULL || $do == NULL || $b == NULL || $p == NULL || $v == NULL || $o == NULL || $u == NULL || $dr == NULL || $er == NULL )
{
	echo "<br><br>";
	echo "<h4>Sorry, this month/year doest not exist in database.</h4>";
	echo "<a href = 'edit.php'>Back</a>";
	return;
}

// Convert number to month
$monthStr = array( 1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");

// Preenche formularios
echo "<h4>Assets in " . $monthStr[$month] . "/" . $year . "</h4>";
echo "<br>";
echo "<form action='edit.php' action='edit.php' method='get'>";
echo "2Qtos C. Grande: <input type='text' name='cg' value='" . $cg . "'><br>";
echo "2Qtos Curicica: <input type='text' name='cu' value='" . $cu . "'><br>";
echo "Gold: <input type='text' name='g' value='" . $g . "'><br>";
echo "Silver: <input type='text' name='s' value='" . $s . "'><br>";
echo "Dow: <input type='text' name='do' value='" . $do . "'><br>";
echo "iBovespa: <input type='text' name='b' value='" . $b . "'><br>";
echo "PETR4: <input type='text' name='p' value='" . $p . "'><br>";
echo "VALE5: <input type='text' name='v' value='" . $v . "'><br>";
echo "OGXP3: <input type='text' name='o' value='" . $o . "'><br>";
echo "USIM5: <input type='text' name='u' value='" . $u . "'><br>";
echo "Dollar: <input type='text' name='dr' value='" . $dr . "'><br>";
echo "Euro: <input type='text' name='er' value='" . $er . "'><br>";
echo "<br>";
echo "<input type='hidden' name='update'>";
echo "<input type='hidden' name='month' value = $month>";
echo "<input type='hidden' name='year' value = $year>";
echo "<input type='submit' value='Update'>";
echo "</form>";

?> 

</body>
</html>
